package com.x.bff.app.service.cms;

import com.x.bff.app.model.vo.cms.ContentCreateReqVO;
import com.x.bff.app.model.vo.cms.ItemToggleRelationSaveReqVO;
import com.x.core.utils.ApiAssetUtil;
import com.x.core.utils.BeanUtil;
import com.x.core.utils.CompareUtils;
import com.x.core.web.api.ResultCode;
import com.x.core.web.page.PageList;
import com.x.provider.api.common.enums.ItemTypeEnum;
import com.x.provider.api.general.enums.ItemToggleRelationTypeEnum;
import com.x.provider.api.general.model.dto.cms.*;
import com.x.provider.api.general.service.CmsRpcService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ContentServiceImpl implements ContentService{

    private final CmsRpcService cmsRpcService;

    public ContentServiceImpl(CmsRpcService cmsRpcService){
        this.cmsRpcService = cmsRpcService;
    }

    @Override
    public void create(ContentCreateReqVO reqVO) {
        List<ContentTitleItemCreateReqDTO> contentTitleItemCreateReqDTO = BeanUtil.prepare(reqVO.getContentTitleItemList(), ContentTitleItemCreateReqDTO.class);
        ContentCreateReqDTO contentCreateReqDTO = BeanUtil.prepare(reqVO, ContentCreateReqDTO.class);
        contentCreateReqDTO.setContentTitleItemList(contentTitleItemCreateReqDTO);
        cmsRpcService.createContent(contentCreateReqDTO);
    }

    @Override
    public void toggleRelation(ItemToggleRelationSaveReqVO reqVO) {
        if (ItemTypeEnum.CUSTOMER.getValue() == reqVO.getFromItemType()){
            ApiAssetUtil.isTrue(CompareUtils.gtZero(reqVO.getSessionCustomerId()), ResultCode.UNAUTHORIZED);
            reqVO.setFromItemId(reqVO.getSessionCustomerId());
        }
        ItemToggleRelationSaveReqDTO reqDTO = BeanUtil.prepare(reqVO, ItemToggleRelationSaveReqDTO.class);
        cmsRpcService.toggleRelation(reqDTO);
    }

    @Override
    public List<ItemToggleRelationDTO> listCustomerStarRelation(Long fromCustomerId, Long toCustomerId, List<Long> fromCustomerIdList, List<Long> toCustomerIdList) {
        return cmsRpcService.listItemToggleRelation(ItemToggleRelationListReqDTO.builder().fromItemId(fromCustomerId).fromItemIdList(fromCustomerIdList).fromItemType(ItemTypeEnum.CUSTOMER.getValue())
                .toItemId(toCustomerId).toItemIdList(toCustomerIdList).relationType(ItemToggleRelationTypeEnum.STAR.getValue()).build()).getData().getList();
    }

}
